﻿Imports HMSLibrary
Imports Microsoft.ApplicationBlocks.Data
Imports System.Data
Imports HMSDataLayer
Imports System.Collections.ObjectModel

Public Class PrescriptionTemplateControl
    Implements IHMSData

    Public Property ID As Integer Implements HMSLibrary.IHMSData.ID

    Public Function DataValidation() As Boolean Implements HMSLibrary.IHMSData.DataValidation
        DataValidation = False
        Try
            If Trim(NameTextBox.Text) = "" Then
                CommonMessages.ShowValidationMsg("Please enter template name!")
                Exit Function
            End If

            If ActionMode = FormAction.ADD Then
                Dim strSql As String
                strSql = "SELECT  [BillTemplateID] FROM dbo.BillTemplates WHERE  ([TemplateName] = N'" & NameTextBox.Text.Trim & "') "
                Dim objTemp As Object = Nothing
                objTemp = AppCommon.db.ExecuteScalar(CommandType.Text, strSql)
                If objTemp IsNot Nothing Then
                    If IsDBNull(objTemp) = False Then
                        CommonMessages.ShowValidationMsg("Template already exist!")
                        Exit Function
                    End If
                End If
            End If
            DataValidation = True
        Catch ex As Exception
            AppCommon.LogError(ex)
        End Try
    End Function

    Public Sub LoadData(ByVal ID As Integer) Implements HMSLibrary.IHMSData.LoadData
        ActionMode = FormAction.EDIT
    End Sub

    Public Function SaveData() As Boolean Implements HMSLibrary.IHMSData.SaveData
        If DataValidation() = False Then Return False
        Dim dataProvider As New MasterDataLayer()
        PresTemp.prescription = NameTextBox.Text
        PresTemp.remarks = DescriptionTextBox.Text
        If dataProvider.SavePrescriptionTemplate(PresTemp, ActionMode) = True Then
            Return True
        End If
    End Function

    Public Property ActionMode As HMSLibrary.FormAction Implements HMSLibrary.IHMSData.ActionMode
    Dim lstMed As New List(Of MedicineItemsDtl)

    Private Sub ClientEntryUserControl_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim dataProvider As New MasterDataLayer()
        Dim lst As List(Of Lookups)
        lst = dataProvider.GetLookupList(LookupTypes.MedicineFrequency)
        FrequencyComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.Periods)
        PeriodComboBox.ItemsSource = lst

        lst = dataProvider.GetLookupList(LookupTypes.GenericDrug)
        lst.Insert(0, New Lookups With {.ID = 0, .Name = "All"})
        GenericDrugComboBox.ItemsSource = lst
        GenericDrugComboBox.SelectedIndex = 0

        lst = dataProvider.GetLookupList(LookupTypes.MedicineManufacturer)
        lst.Insert(0, New Lookups With {.ID = 0, .Name = "All"})
        ManufacturerComboBox.ItemsSource = lst
        ManufacturerComboBox.SelectedIndex = 0

        lst = dataProvider.GetLookupList(LookupTypes.MedicineType)
        lst.Insert(0, New Lookups With {.ID = 0, .Name = "All"})
        MedicineTypeComboBox.ItemsSource = lst
        MedicineTypeComboBox.SelectedIndex = 0

        lstMed = dataProvider.GetMedicineLookupItems()
        MedicineComboBox.ItemsSource = lstMed
        AddNew()
    End Sub

    Dim PresTemp As PrescriptionTemplate

    Public Sub AddNew() Implements HMSLibrary.IHMSData.AddNew
        NameTextBox.Text = String.Empty
        PresTemp = New PrescriptionTemplate
        ActionMode = FormAction.ADD
        PresTemp.ItemsList = New ObservableCollection(Of PatientPrescription)
        TemplateDataGrid.ItemsSource = PresTemp.ItemsList
    End Sub

    Public Sub FindData() Implements HMSLibrary.IHMSData.FindData
        MainGrid.Visibility = Visibility.Collapsed
        FindStackPanel.Visibility = Visibility.Visible
        Dim DataObject As MasterDataLayer
        Dim DataCollection As Object
        DataObject = New MasterDataLayer()
        DataCollection = DataObject.GetUserProfile
        SearchDataGrid.ItemsSource = DataCollection
    End Sub

    Public Sub BackView() Implements HMSLibrary.IHMSData.BackView
        MainGrid.Visibility = Visibility.Visible
        FindStackPanel.Visibility = Visibility.Collapsed
    End Sub

    Private Sub DeleteButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    End Sub

    Private Sub AddPrescriptionButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AddPrescriptionButton.Click
        If Trim(MedicineComboBox.Text) = "" Then
            CommonMessages.ShowValidationMsg("Please select medicine!")
            Exit Sub
        End If
        Dim item As New PatientPrescription
        item.medicineItem = MedicineComboBox.Text
        item.medicineItemID = MedicineComboBox.SelectedValue
        item.dosage = Val(DosageTextBox.Text)
        item.duaration = Val(DurationTextBox.Text)
        item.frequency = FrequencyComboBox.SelectedValue
        item.frequencyName = FrequencyComboBox.Text
        item.periods = PeriodComboBox.Text
        PresTemp.ItemsList.Add(item)
    End Sub

    Private Sub GenericDrugComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles GenericDrugComboBox.SelectionChanged
        MedicineFilter()
    End Sub

    Private Sub ManufacturerComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ManufacturerComboBox.SelectionChanged
        MedicineFilter()
    End Sub

    Private Sub MedicineTypeComboBox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles MedicineTypeComboBox.SelectionChanged
        MedicineFilter()
    End Sub

    Private Sub MedicineFilter()
        Dim lst As List(Of MedicineItemsDtl)
        lst = lstMed
        If GenericDrugComboBox.SelectedValue > 0 Then
            lst = (From l In lst Where l.genericDrug = GenericDrugComboBox.SelectedValue Select l).ToList
        End If
        If MedicineTypeComboBox.SelectedValue > 0 Then
            lst = (From l In lst Where l.medicineType = MedicineTypeComboBox.SelectedValue Select l).ToList
        End If
        If ManufacturerComboBox.SelectedValue > 0 Then
            lst = (From l In lst Where l.mfg = ManufacturerComboBox.SelectedValue Select l).ToList
        End If
        MedicineComboBox.ItemsSource = lst
    End Sub
End Class
